package com.dynamic.manager.controller.admin.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dynamic.model.model.po.SysmanRole;
import com.dynamic.model.model.po.SysmanUser;
import com.dynamic.model.service.SystemRoleService;
import com.dynamic.model.service.SystemUserService;
import org.apache.log4j.Logger;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dynamic.model.common.action.BaseController;
import com.dynamic.model.common.service.BaseService;
import com.dynamic.model.common.vo.Page;
import com.dynamic.model.common.vo.Result;

/**
 * 
 * @desc 用户后台登陆用户表--RBAC权限管理action控制类
 *
 * @author luwei
 */
@Controller
@RequestMapping(value="admin/systemuser")
public class SystemUserController extends BaseController<SysmanUser>  {

	private static final Logger LOGGER = Logger.getLogger(SysmanUser.class);
	
	@Override
	@Resource(name = "systemUserServiceImpl")
	public void setBaseService(BaseService<SysmanUser> baseService) {
		this.baseService = baseService;
	}
	
	private SystemUserService getService(){
		return (SystemUserService) this.baseService;
	}
	
	@Resource(name = "systemRoleServiceImpl")
	private SystemRoleService sysmanRoleService;
	
	/**
	 * 重写list方法，在进入list页面的时候，将sysmanRoleList 加载到页面中
	 */
	@Override
	public ModelAndView list(ModelAndView model) {
		List<SysmanRole> sysmanRoleList = sysmanRoleService.getAll();
		model.addObject("sysmanRoleList", sysmanRoleList);
		return super.list(model);
	}
	
	@RequestMapping(value = "touserlist")
	@ResponseBody
	public ModelAndView touserlist(ModelAndView model) {
		ModelAndView mav = new ModelAndView();
		Map<String,String> nav=new LinkedHashMap<String,String>();
		nav.put("后台","#");
		nav.put("系统管理","#");
		nav.put("用户列表","/admin/systemuser/touserlist");
		mav.addObject("navmap", nav);
		mav.setViewName("jsp/systemuser");
		return mav;
	}
	
	@Override
	protected Result beforeDoSave(HttpServletRequest request,HttpServletResponse response, SysmanUser entity) throws Exception {
		//加密密码
		SysmanUser user=this.getService().findByName(entity.getUserName());
		if(null!=user){
			Result result=new Result();
			result.setSuccess(false);
			result=Result.fail("账户不能重复");
			return result;
		}
		Md5Hash md5Hash = new Md5Hash(entity.getPassword());
		entity.setPassword(md5Hash.toHex());
		
		loadEntityRoles(request, entity);
		
		super.beforeDoSave(request,response, entity);
		return Result.ok();
	}
	
	@Override
	protected Result beforeDoUpdate(HttpServletRequest request, SysmanUser entity) throws Exception {
		//加密密码
		Md5Hash md5Hash = new Md5Hash(entity.getPassword());
		entity.setPassword(md5Hash.toHex());
		loadEntityRoles(request, entity);
		super.beforeDoUpdate(request, entity);
		return Result.ok();
	}
	
	/**
	 * 向实体对象加载关联关系（用户包含的角色信息）
	 * @param request 请求参数
	 * @param entity form表单提交的实体对象
	 */
	private void loadEntityRoles(HttpServletRequest request, SysmanUser entity) {
		String relRolePids = request.getParameter("relRolePids");
		if(relRolePids != null && !relRolePids.equals("")) {
			String[] idArr = relRolePids.split(",");
			
			List<SysmanRole> list = new ArrayList<SysmanRole>();
			for(String id : idArr){
				SysmanRole _role = new SysmanRole();
				_role.setPid(Integer.parseInt(id));
				list.add(_role);
			}
			
			entity.setRoles(list);
		}
	}
	
	@RequestMapping(value = "userlist")
	@ResponseBody
	public void rolelist(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String order = request.getParameter("order");
		String statu = request.getParameter("statu");
		String username = request.getParameter("departmentname1");
		String realname = request.getParameter("departmentname2");
		if(null!=offset&&null!=limit){
			Page<SysmanUser> page=new Page<SysmanUser>();
			page.setOffset(Integer.parseInt(offset));
			page.setLimit(Integer.parseInt(limit));
			page.setOrder(order);
			List<SysmanUser> userlist=this.getService().getPageDataByName(page, username,realname,statu);
			page.setRows(userlist);
			this.renderJson(page, response);
		}else{
			Result result=new Result();
			result=Result.fail("参数错误");
			this.renderJson(result, response);
		}
	}
}
